package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.handler.UserInfoRedisHandler;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.website.util.UserContext;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2019/7/5.
 */
 /*1>参数为空
   2>手机唯一校验
   3>手机格式校验[拓展:手机格式校验-提示: java端正则表达式]
   4>短信验证码校验.*/
@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @ResponseBody
    @RequestMapping("/checkPhone")
    public boolean checkPhone(String phone) {
        return !userInfoService.checkPhone(phone);
    }

    @ResponseBody
    @RequestMapping("/userRegist")
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return new AjaxResult();
    }

    @ResponseBody
    @RequestMapping("/userLogin")
    public AjaxResult userLogin(HttpServletResponse response,String username, String password) {
        //接收用户输入过来的账号密码,进行后台校验
        String uuid = userInfoService.checkLogin(username, password);
        String token = UserInfoRedisHandler.USER_INFO_REDIS_HANDLER.getKeyPrefix()+uuid;

        //将redis的key作为cookie中的value,然后key值为一个常量
        UserContext.addCookie(response,UserInfoRedisHandler.TOKEN_COOKIE,token);
        //不仅仅是项目之间的共享redis,单个项目中也要保存到session中去

        //登录成功以后,需要将用户信息存入当前服务器的session中
        UserContext.getSession().setAttribute("userInfo",userInfoRedisService.get(UserInfoRedisHandler.USER_INFO_REDIS_HANDLER,token));
        return new AjaxResult();
    }

}
